﻿<CodeSnippetTabbed DocLink="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxDataGrid-1.Data">
    <CodeSnippetTabPage Text="Razor">
        @(@"<DxDataGrid Data=""@DataSource""
            PageSize=""5"">
    <DxDataGridDateEditColumn Field=""@nameof(WeatherForecast.Date)"" DisplayFormat=""D"" />
    <DxDataGridColumn Field=""@nameof(WeatherForecast.Forecast)"" />
    <DxDataGridSpinEditColumn Field=""@nameof(WeatherForecast.TemperatureC)"" Caption=""@(""Temp. (\x2103)"")""
                              TextAlignment=""DataGridTextAlign.Left"" Width=""150px"" />
    <DxDataGridSpinEditColumn Field=""@nameof(WeatherForecast.TemperatureF)"" Caption=""@(""Temp. (\x2109)"")""
                              TextAlignment=""DataGridTextAlign.Left"" Width=""150px"" />
</DxDataGrid>
@code {
    [Inject] protected WeatherForecastService ForecastService { get; set; }
    IEnumerable<WeatherForecast> DataSource = Enumerable.Empty<WeatherForecast>();

    protected override void OnInitialized() {
        DataSource = ForecastService.GetForecast();
    }
}")
    </CodeSnippetTabPage>
    <CodeSnippetTabPage Text="WeatherForecast" Language="csharp">
        @(@"using System;

public class WeatherForecast {
    public DateTime Date { get; set; }
    public int TemperatureC { get; set; }
    public string Forecast { get; set; }
    public double TemperatureF => Math.Round((TemperatureC * 1.8 + 32), 2);
}")
    </CodeSnippetTabPage>
    <CodeSnippetTabPage Text="WeatherForecastService" Language="csharp">
        @(@"using System;
using System.Collections.Generic;
using System.Linq;

public class WeatherForecastService {
    private List<WeatherForecast> Forecasts { get; set; }

    public WeatherForecastService() {
        Forecasts = CreateForecast();
    }

    public IEnumerable<WeatherForecast> GetForecast() {
        return Forecasts;
    }

    private List<WeatherForecast> CreateForecast() {
        var rng = new Random();;
        DateTime startDate = DateTime.Now;

        int min = 0;
        int max = 0;
        int? temperatureC = null;

        return Enumerable.Range(1, 15).Select(index => {
            var day = startDate.AddDays(index);
            if(day.Month - startDate.AddDays(index - 1).Month != 0 || temperatureC == null) {
                min = temperatureStatistic[day.Month - 1].Item1;
                max = temperatureStatistic[day.Month - 1].Item2;
                temperatureC = rng.Next(min, max);
            } else {
                temperatureC = temperatureC + rng.Next(-3, 3);
                temperatureC = Math.Max(temperatureC.Value, min);
                temperatureC = Math.Min(temperatureC.Value, max);
            }
            int weatherTypes = rng.NextDouble() < 0.5 ? 0 : (rng.NextDouble() < 0.5 ? 1 : rng.NextDouble() < 0.5 ? 2 : 3);
            double feelTemper = temperatureC.Value - (weatherTypes - 1) * 3;
            return new WeatherForecast {
                Date = day,
                TemperatureC = temperatureC.Value,
                Forecast = ConditionsForForecast.First(c => c.Item1 <= feelTemper).Item2
            };
        }).ToList();
    }

    Tuple<int, int>[] temperatureStatistic = new Tuple<int, int>[] {
        Tuple.Create(-15, -1),
        Tuple.Create(-12, -2),
        Tuple.Create(-6, 8),
        Tuple.Create(4, 14),
        Tuple.Create(11, 21),
        Tuple.Create(13, 23),
        Tuple.Create(18, 32),
        Tuple.Create(16, 26),
        Tuple.Create(10, 18),
        Tuple.Create(1, 11),
        Tuple.Create(-4, 6),
        Tuple.Create(-13, 1)
    };

    Tuple<int, string>[] ConditionsForForecast = new Tuple<int, string>[] {
        Tuple.Create( 28 , ""Hot""),
        Tuple.Create(14, ""Warm""),
        Tuple.Create(-9, ""Cold""),
        Tuple.Create(-80, ""Freezing"")
    };
}")
    </CodeSnippetTabPage>
 <CodeSnippetTabPage Text="Startup" Language="csharp">
        @(@"public class Startup {
    public void ConfigureServices(IServiceCollection services) {
        // ...
        services.AddSingleton<WeatherForecastService>();
    }
}")
    </CodeSnippetTabPage>
</CodeSnippetTabbed>
